package top.kaoshanji.p1ch02e;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.SwitchPoint;

/**
 * 代码清单 p1ch02e-03：SwitchPoint的基本使用示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/4 19:11
 */
public class SwitchPointExample01 {

    private static final Logger logger = LogManager.getLogger(SwitchPointExample01.class);

    public static void main(String[] args) throws Throwable {
        logger.info("...代码清单 p1ch02e-03：SwitchPoint的基本使用示例...");

        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodType type = MethodType.methodType(int.class, int.class, int.class);
        MethodHandle mhMax = lookup.findStatic(Math.class, "max", type);
        MethodHandle mhMin = lookup.findStatic(Math.class, "min", type);

        SwitchPoint sp = new SwitchPoint();
        MethodHandle mhNew = sp.guardWithTest(mhMin, mhMax);

        int value1 = (int)mhNew.invoke(3, 4);
        System.out.println(value1);

        SwitchPoint.invalidateAll(new SwitchPoint[]{sp});

        int value2 = (int)mhNew.invoke(3, 4);
        System.out.println(value2);


    }

}
